const { getTopicList} = require('../../api/index.js')
const app = getApp()
Component({
  data: {
    topicParams: {
      page: 1,
      size: 3
    },
    topicObj: {
      data: []
    },
    freshStatus: false,
    winWidth: 0,
    winHeight: 0
  },
  methods: {
    // getTopicList() { //获取专题列表
    //   wx.showLoading({
    //     title: '加载中',
    //   })
    //   const {page,size} = this.data.topicParams;
    //   getTopicList({page,size}).then(res => {
    //     this.setData({
    //       topicObj: res
    //     })
    //     console.log(res)
    //   })
    // },
    // getNextPage() { //加载下一页
    //   let {page,size} = this.data.topicParams;
    //   getTopicList({ 
    //     page: page + 1, 
    //     size 
    //   }).then(res => {
    //     this.setData({
    //       topicObj: res,
    //       'topicParams.page': page + 1
    //     })
    //   })
    // },
    // getPrePage() { //加载上一页
    //   let { page, size } = this.data.topicParams;
    //   getTopicList({
    //     page: page - 1,
    //     size
    //   }).then(res => {
    //     this.setData({
    //       topicObj: res,
    //       'topicParams.page': page - 1
    //     })
    //   })
    // },
    getList(e) { //综合写法
      let num = e ? e.currentTarget.dataset.num : 0;
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        topicObj: {
          data: []
        }
      })
      const { page, size } = this.data.topicParams;
      let str = `topicObj.data[${this.data.topicObj.data.length}]`
      getTopicList({ page: page + num, size }).then(res => {
        this.setData({ //解决setData超过1M问题
          'topicObj.count': res.count,
          'topicObj.currentPage': res.currentPage,
          'topicObj.pageSize': res.pageSize,
          'topicObj.totalPages': res.totalPages,
          [str]: res.data,
          'topicParams.page': res.currentPage
        })
      })
    },
    getListMore() { //到达底部加载更多
      const { page, size } = this.data.topicParams;
      const { totalPages } = this.data.topicObj;
      if(page < totalPages) {
        getTopicList({ page: page + 1, size }).then(res => {
          let str = `topicObj.data[${this.data.topicObj.data.length}]`
          console.log(str)
          this.setData({
            'topicObj.count': res.count,
            'topicObj.currentPage': res.currentPage,
            'topicObj.pageSize': res.pageSize,
            'topicObj.totalPages': res.totalPages,
            [str]: res.data,
            'topicParams.page': res.currentPage
          })
        })
      }
    },
    onReachBottom() { //页面上拉触底事件的处理函数
      this.getListMore()
    },
    onPullDownRefresh() { //监听用户下拉动作
      this.setData({
        'topicParams.page': 1
      })
      this.getList();
      setTimeout(()=>{
        wx.stopPullDownRefresh(); //停止下拉
      },1000)
    },
    setPullDownRefresh() { //scrollview自定义下拉刷新
      this.setData({
        'topicParams.page': 1
      })
      this.getList();
      setTimeout(()=>{
        this.setData({
          freshStatus: false
        })
      },1500)
    },
    setFinish() { //自定义下拉刷新被复位
      console.log('加载完成')
    }
  },
  pageLifetimes: {
    show() { //组件所在的页面被展示时执行
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
      // 调用app.js
      this.setData({
        inavHeight: app.globalData.inavHeight,
        inavTop: app.globalData.inavTop
      })
      // 获取当前设备的宽高
      var that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            winWidth: res.windowWidth,
            winHeight: res.windowHeight
          });
        }
      });
    }
  },
  lifetimes: {
    attached: function () { // 在组件实例进入页面节点树时执行
      // this.getTopicList()
      this.getList()
    }
  }
})
